package com.zigong.app.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;
import com.zigong.app.R;
import com.zigong.app.beans.ChooseCityVo;
import com.zigong.app.beans.FocusColumn;
import com.zigong.app.beans.HighRiseBean;
import com.zigong.app.beans.HighRiseImage;
import com.zigong.app.beans.HighRiseCharacterBean;
import com.zigong.app.beans.HighRiseNewsInfo;
import com.zigong.app.beans.HighRiseTypeBean;
import com.zigong.app.beans.HighRiseUser;
import com.zigong.app.beans.HighRiseVo;
import com.zigong.app.beans.NewsItem;
import com.zigong.app.http.AbsListNetCallback;
import com.zigong.app.http.AbsNetCallBack;
import com.zigong.app.http.NetUtils;
import com.zigong.app.ui.adapter.HighRiseAdapter;
import com.zigong.app.ui.adapter.HighRiseNewsListAdapter;
import com.zigong.app.ui.util.IListShowData;
import com.zigong.app.utils.ListTypeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HighRiseFragment extends ColumnBaseFragment implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener {

    @BindView(R.id.fm_high_rise_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView recyclerView;

    private int userSize = 0;
    private HighRiseAdapter adapter;

    private List<IListShowData> list;

    public HighRiseFragment() {
        // Required empty public constructor
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_high_rise, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        adapter = new HighRiseAdapter(getContext(), this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onFragmentUnShowed(boolean isCallFromPause) {
        super.onFragmentUnShowed(isCallFromPause);

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {

        }
    }


    @Override
    public void onPause() {
        super.onPause();

    }

    @Override
    protected boolean onFragmentShow(boolean isFirst, boolean isCallFromResume) {
        if (isFirst) {
            refreshLayout.autoRefresh();
        }
        return super.onFragmentShow(isFirst, isCallFromResume);
    }

    @Override
    public void onLoadMore() {
        refreshLayout.loadMoreComplete();
    }

    @Override
    public void onRefresh() {
        getChannels();
    }

    /**
     * 请求基础的路径数据
     */
    private void getChannels() {
        NetUtils.getNetAdapter().getHighRiseList(getOwnerName(), channelUrl, new AbsNetCallBack<HighRiseBean>(HighRiseBean.class) {

            @Override
            public void onSuc(HighRiseBean vo) {
                if (null != vo ) {
                    initData(vo );
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
            }

            @Override
            public void onEnd() {
                super.onEnd();
                refreshLayout.refreshComplete();
            }
        });
    }

    /**
     * 初始化 高层数据
     * @param data
     */
    private void initData(HighRiseBean data) {

        list = new ArrayList<>();
        HighRiseImage bean = new HighRiseImage();
        bean.setViewType(HighRiseAdapter.HIGH_RISE_IMAGE);
        bean.setImageUrl(data.getImage());
        list.add(bean);
        if (null != data.getUserList()) {
            HighRiseCharacterBean cBean;
            userSize = data.getUserList().size();
            for (int i = 0; i < data.getUserList().size(); i++) {
                HighRiseUser user = data.getUserList().get(i);
                cBean = new HighRiseCharacterBean();
                cBean.setViewType(HighRiseAdapter.HIGH_RISE_CHARACTER);
                cBean.setIcon(user.getPicture());
                cBean.setId(user.getUserId());
                cBean.setName(user.getUserName());
                cBean.setPost(user.getPosition());
                cBean.setIntroJson(user.getPersonInfo());
                cBean.setTempSpecialJson(user.getSpecialInfo());
                List<NewsItem> l = user.getNewsList();
                if (null != l) {
                    List<HighRiseNewsInfo> newsList = new ArrayList<>();
                    HighRiseNewsInfo info;
                    for (int k = 0; k < l.size(); k++) {
                        info = new HighRiseNewsInfo(HighRiseNewsListAdapter.NEWS_TYPE, "", l.get(k));
                        newsList.add(info);
                    }
                    info = new HighRiseNewsInfo(HighRiseNewsListAdapter.LOAD_MORE_TYPE, "查看更多", null);
                    info.setMoreUrl(user.getMoreNewsUrl());
                    newsList.add(info);
                    cBean.setNewsList(newsList);
                }
                list.add(cBean);
            }
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) instanceof HighRiseCharacterBean) {
                    HighRiseCharacterBean b = (HighRiseCharacterBean) list.get(i);
                    getSpecialList(b.getTempSpecialJson(), i);
                }
            }


        } else {
            adapter.setDataList(list);
        }
    }

    /**
     * 获取高层人员的新闻子专题类型
     * @param url
     * @param position
     */
    private void getSpecialList(String url, final int position) {
        NetUtils.getNetAdapter().getNewsArray(getOwnerName(), url, new AbsListNetCallback<NewsItem>(NewsItem.class) {
            @Override
            public void onSuc(List<NewsItem> datas) {
                ArrayList<NewsItem> subSpecialList = getPageResponse().getData().getSubSpecialList();
                if (ListUtils.isListValued(subSpecialList)) {
                    List<HighRiseTypeBean> typeList = new ArrayList<>();
                    HighRiseTypeBean typeBean;
                    for (NewsItem item : subSpecialList) {
                        typeBean = new HighRiseTypeBean();
                        typeBean.setType(item.getNewsId());
                        typeBean.setTypeName(item.getNewsTitle());
                        typeBean.setTypeUrl(item.getNewsUrl());
                        typeList.add(typeBean);
                    }
                    if (list.get(position) instanceof HighRiseCharacterBean && typeList.size() > 0) {
                        HighRiseCharacterBean b = (HighRiseCharacterBean) list.get(position);
                        b.setNewsTypeList(typeList);
                    }
                }

            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
            }

            @Override
            public void onEnd() {
                userSize--;
                if(userSize ==0){
                    adapter.setDataList(list);
                }
            }
        });
    }


    OnItemInternalClick onItemSubscribeClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {

        }
    };

}
